package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.service.IStrategyEsService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVo;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {


    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;

    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        //url传入中文时,会自动讲中文进行编码,此时需要解码
        //解码成中文
        String kw = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(kw);


        switch (qo.getType()) {
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return searchUser(qo);
             default:
                 return searchAll(qo);
        }
    }

    //查目的地
    private Object searchDest(SearchQueryObject qo) {
        //判断输入的keyword是不是查询目的地
        Destination dest = destinationService.queryByName(qo.getKeyword());

        //如果是,查询目的地下的所有游记,攻略,用户
        //result    total  strategys  travels users
        SearchResultVo vo = new SearchResultVo();
        if(dest!=null){
            //攻略
            List<Strategy> strategies = strategyService.queryByDestName(qo.getKeyword());
            vo.setStrategys(strategies);
            //游记
            List<Travel> travels = travelService.queryByDestName(qo.getKeyword());
            vo.setTravels(travels);
            //用户
            List<UserInfo> userInfos = userInfoService.queryByDestName(qo.getKeyword());
            vo.setUsers(userInfos);

            long total = strategies.size()+travels.size()+userInfos.size();
            vo.setTotal(total);
        }

        //qo
        //dest
        Map<String,Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        map.put("dest",dest);
        return JsonResult.success(map);

    }

    //查攻略
    private Object searchStrategy(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索(结果需要高亮)

        //全文搜索+显示高亮
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class,
                qo, "title", "subTitle", "summary");

        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);

    }


    //查用户
    private Object searchUser(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索(结果需要高亮)

        //全文搜索+显示高亮
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class,
                qo, "info", "city");

        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);

    }

    //查游记
    private Object searchTravel(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索(结果需要高亮)

        //全文搜索+显示高亮
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class,
                qo, "title", "summary");

        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }




    //查所有
    private Object searchAll(SearchQueryObject qo) {

        SearchResultVo vo = new SearchResultVo();

        long total =0L;  //搜索结果总条数
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class,
                qo, "title", "subTitle", "summary");

        vo.setStrategys(sts.getContent());
        total +=sts.getTotalElements();  //攻略分页总记录数

        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class,
                qo, "title", "summary");

        vo.setTravels(ts.getContent());
        total +=ts.getTotalElements();


        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class,
                qo, "info", "city");

        vo.setUsers(us.getContent());
        total +=us.getTotalElements();

        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class,
                qo, "name", "info");

        vo.setDests(ds.getContent());
        total +=ds.getTotalElements();

        vo.setTotal(total);
        Map<String,Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        return JsonResult.success(map);
    }





}
